home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15706 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  58 lines

  1. Path: ix.netcom.com!news
  2. From: jhewett@ix.netcom.com (Jerry Hewett)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Normalized Random Nos.
  5. Date: Sat, 06 Apr 96 23:39:26 GMT
  6. Organization: Netcom
  7. Message-ID: <N.040696.153926.84@ix.netcom.com>
  8. References: <4k4gmh$7ol@fountain.mindlink.net>
  9. NNTP-Posting-Host: tem-ca1-12.ix.netcom.com
  10. X-NETCOM-Date: Sat Apr 06  5:39:54 PM CST 1996
  11. X-Newsreader: Quarterdeck Message Center [2.00]
  12.  
  13. John (jswalby@mindlink.bc.ca) asks:
  14.  
  15. > Does anybody out there have a C or C++ (msvc++) algorithm to generate
  16. > normalized random numbers (i.e. between 0 and 1) using the rand() function?
  17.  
  18. Try this out -- it's a modified version of the sample program for rand() that 
  19. comes with the MSVC online help:
  20.  
  21. ----<snip>----
  22.  
  23. /* RAND.C: This program seeds the random-number generator
  24.  * with the time, then displays 10 random integers.
  25.  */
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <time.h>
  30.  
  31. void main( void )
  32. {
  33.    float    result;
  34.    int      i;
  35.  
  36.    /* Seed the random-number generator with current time so that
  37.     * the numbers will be different every time we run.
  38.     */
  39.    
  40.    srand( (unsigned)time( NULL ) );
  41.  
  42.    /* Display 10 numbers. */
  43.    
  44.    for(i = 0;i < 10;i++ )
  45.    {
  46.        result = (float) rand ();
  47.        printf ("%f\n",result / RAND_MAX);
  48.    }
  49. }
  50.  
  51. ----<snip>----
  52.  
  53. Just guessing here, but do you need this for a fuzzy logic app?
  54.  
  55. Jerry H.
  56.  
  57.  
  58.